using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Entities;
using System.Configuration;

namespace DAL
{
  public   class tbHinhSanPhamDAL
    {
      private SqlConnection cn = null;
        public tbHinhSanPhamDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public tbHinhSanPhamDAL(tbHinhSanPhamDAL dao)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
      public void Insert(tbHinhSanPham hinhsp)
      {
          SqlCommand cm = new SqlCommand("tbHinhSanPham_Insert", cn);
          cm.CommandType = CommandType.StoredProcedure;
          cm.Parameters.AddWithValue("@Duongdan", hinhsp.Duongdan );
          cm.Parameters.AddWithValue("@TenHinh", hinhsp.TenHinh);
          cm.ExecuteNonQuery();
      }
      public void Update(tbHinhSanPham hinhsp)
      {
          SqlCommand cm = new SqlCommand("tbHinhSanPham_Update", cn);
          cm.CommandType = CommandType.StoredProcedure;
          cm.Parameters.AddWithValue("@MaHinh", hinhsp.MaHinh);
          cm.Parameters.AddWithValue("@Duongdan", hinhsp.Duongdan);
          cm.Parameters.AddWithValue("@TenHinh", hinhsp.TenHinh);
          cm.ExecuteNonQuery();
      }
      public void Delete(int ID)
      {
          SqlCommand cm = new SqlCommand("tbHinhSanPham_Detele", cn);
          cm.CommandType = CommandType.StoredProcedure;
          cm.Parameters.AddWithValue("@MaHinh", ID);
          cm.ExecuteNonQuery();

      }
      public tbHinhSanPhamCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
      {
          SqlCommand cm = new SqlCommand("tbHinhSanPham_GET", cn);
          cm.CommandType = CommandType.StoredProcedure;
          cm.Parameters.AddWithValue("@Where", where);
          cm.Parameters.AddWithValue("@PageIndex", pageIndex);
          cm.Parameters.AddWithValue("@PageSize", pageSize);
          cm.Parameters.AddWithValue("@OrderBy", orderBy);
          cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
          cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;
          SqlDataReader dr = cm.ExecuteReader();
          tbHinhSanPhamCollection hinhSanPhamCollection = new tbHinhSanPhamCollection();
          while (dr.Read())
          {
              tbHinhSanPham HinhSanPham = new tbHinhSanPham();
             HinhSanPham .MaHinh  = int.Parse(dr.GetValue(0).ToString());
             HinhSanPham .Duongdan  = dr.GetValue(1).ToString();
             HinhSanPham.TenHinh = dr.GetValue(2).ToString();
             hinhSanPhamCollection.Add(HinhSanPham);
          }
          dr.Close();
          totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
          return hinhSanPhamCollection;
      }
    }
}
